package taller;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author 17298669-2
 */
public class gui_mantencion extends javax.swing.JInternalFrame {

    /**
     * Creates new form mecanico_gui
     */
    public gui_mantencion() {
        initComponents();
        cargar();
    }
    consultas p=new consultas();
    ///
    Object[][] dtMant;  
    private void cargar() {
        //objeto para almacenar datos
        Object[][] dtMant;        
        String[] columNames = {"Secuencia","Descripcion","Fecha_recepcion","Fecha_entrega", "Folio"};  
        // se utiliza la funcion
        dtMant = p.obtenerMantencion();
        // se colocan los datos en la tabla
        DefaultTableModel datos = new DefaultTableModel(dtMant,columNames);                        
        tblmantencion.setModel(datos);        
    }
        Object[][] dtPer; 
        int fila = -1;
    
    //
    
         private void updateTabla(){             
        String[] columNames = {"Secuencia","Descripcion","Fecha_recepcion","Fecha_entrega","Folio"};   
        dtMant = cc.getCliente();
        DefaultTableModel datos = new DefaultTableModel(dtMant,columNames);                        
        tblmantencion.setModel(datos); 
    }
consultas cc=new consultas();    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtsecuencia = new javax.swing.JTextField();
        txtdescripcion = new javax.swing.JTextField();
        txtfecharecepcion = new javax.swing.JTextField();
        txtfechaentrega = new javax.swing.JTextField();
        txtfolio = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        btnagregar = new javax.swing.JButton();
        btneliminar = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JSeparator();
        btnmostrar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblmantencion = new javax.swing.JTable();
        txtmodificar = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);

        jLabel1.setText("Secuencia :");

        jLabel2.setText("Descripcion :");

        jLabel3.setText("Fecha Recepcion:");

        jLabel4.setText("Fecha Entrega:");

        jLabel5.setText("Folio :");

        btnagregar.setText("Agregar");
        btnagregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnagregarActionPerformed(evt);
            }
        });

        btneliminar.setText("Eliminar");
        btneliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btneliminarActionPerformed(evt);
            }
        });

        btnmostrar.setText("Mostrar");
        btnmostrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnmostrarActionPerformed(evt);
            }
        });

        tblmantencion.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        tblmantencion.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Secuencia", "Descripcion", "Fecha Recepcion", "Fecha Entrega", "Folio"
            }
        ));
        tblmantencion.setToolTipText("");
        tblmantencion.setCellSelectionEnabled(true);
        jScrollPane1.setViewportView(tblmantencion);

        txtmodificar.setText("Modificar");
        txtmodificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtmodificarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1)
            .addComponent(jSeparator2)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addGap(28, 28, 28)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtsecuencia, javax.swing.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE)
                            .addComponent(txtdescripcion)
                            .addComponent(txtfecharecepcion)
                            .addComponent(txtfechaentrega)
                            .addComponent(txtfolio)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(btnagregar)
                        .addGap(67, 67, 67)
                        .addComponent(btneliminar)
                        .addGap(57, 57, 57)
                        .addComponent(txtmodificar)
                        .addGap(40, 40, 40)
                        .addComponent(btnmostrar)))
                .addContainerGap(90, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtsecuencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtdescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtfecharecepcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtfechaentrega, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtfolio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnagregar)
                    .addComponent(btneliminar)
                    .addComponent(btnmostrar)
                    .addComponent(txtmodificar))
                .addGap(18, 18, 18)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(96, 96, 96)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(58, 58, 58))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(99, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnagregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnagregarActionPerformed
        
        if (txtsecuencia.getText().length() > 0 && txtdescripcion.getText().length() > 0 
         && txtfecharecepcion.getText().length() > 0 && txtfechaentrega.getText().length() > 0 && txtfolio.getText().length()>0) {
         int secuencia=Integer.parseInt(txtsecuencia.getText());
         String descripcion=txtdescripcion.getText();
         String fecharecepcion=txtfecharecepcion.getText();
         String fechaentrega=txtfechaentrega.getText();
         String folio = txtfolio.getText();
         cc.agregarMantencion(Integer.toString(secuencia), descripcion, fecharecepcion,fechaentrega,folio);
         updateTabla();
        }else{
            JOptionPane.showMessageDialog(null,"No puede ningun Campo vacio");
        }
        
    }//GEN-LAST:event_btnagregarActionPerformed

    private void btneliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btneliminarActionPerformed
        
         if (fila> -1){
            String codigo = String.valueOf(tblmantencion.getValueAt(fila, 0));                        
            p.borrarMantencion(codigo);
            updateTabla();
            fila=-1;
         }
    }//GEN-LAST:event_btneliminarActionPerformed

    private void btnmostrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnmostrarActionPerformed
        // TODO add your handling code here:
        cargar();
        tblmantencion.setVisible(true);
    }//GEN-LAST:event_btnmostrarActionPerformed

    private void txtmodificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtmodificarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtmodificarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnagregar;
    private javax.swing.JButton btneliminar;
    private javax.swing.JButton btnmostrar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTable tblmantencion;
    private javax.swing.JTextField txtdescripcion;
    private javax.swing.JTextField txtfechaentrega;
    private javax.swing.JTextField txtfecharecepcion;
    private javax.swing.JTextField txtfolio;
    private javax.swing.JButton txtmodificar;
    private javax.swing.JTextField txtsecuencia;
    // End of variables declaration//GEN-END:variables
}
